1 /*
2 * Copyright (c) 2009, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24 /*
25 * @test
26 * @bug 1234567
27 * @summary Test BigDecimal.equals() method.
28 * @author xlu
29 */
30
31 import java.math.*;
32 import static java.math.BigDecimal.*;
33
34 public class EqualsTests {
35
36 public static void main(String argv[]) {
37 int failures = 0;
38
39 BigDecimal[][] testValues = {
40 // The even index is supposed to return true for equals call and
41 // the odd index is supposed to return false, i.e. not equal.
42 {ZERO, ZERO},
43 {ONE, TEN},
44
45 {valueOf(Integer.MAX_VALUE), valueOf(Integer.MAX_VALUE)},
46 {valueOf(Long.MAX_VALUE), valueOf(-Long.MAX_VALUE)},
47
48 {valueOf(12345678), valueOf(12345678)},
49 {valueOf(123456789), valueOf(123456788)},
50
51 {new BigDecimal("123456789123456789123"),
52 new BigDecimal(new BigInteger("123456789123456789123"))},
53 {new BigDecimal("123456789123456789123"),
54 new BigDecimal(new BigInteger("123456789123456789124"))},
55
56 {valueOf(Long.MIN_VALUE), new BigDecimal("-9223372036854775808")},
57 {new BigDecimal("9223372036854775808"), valueOf(Long.MAX_VALUE)},
58
59 {valueOf(Math.round(Math.pow(2, 10))), new BigDecimal("1024")},
60 {new BigDecimal("1020"), valueOf(Math.pow(2, 11))},
61
62 {new BigDecimal(BigInteger.valueOf(2).pow(65)),
63 new BigDecimal("36893488147419103232")},
64 {new BigDecimal("36893488147419103231.81"),
65 new BigDecimal("36893488147419103231.811"),
66 }
67 };
68
69 boolean expected = Boolean.TRUE;
70 for (BigDecimal[] testValuePair : testValues) {
71 failures += equalsTest(testValuePair[0], testValuePair[1], expected);
72 expected = !expected;
73 }
74
75 if (failures > 0) {
76 throw new RuntimeException("Inccured " + failures +
77 " failures while testing equals.");
78 }
79 }
80
81 private static int equalsTest(BigDecimal l, BigDecimal r, boolean expected) {
82 boolean result = l.equals(r);
83 int failed = (result == expected) ? 0 : 1;
84
85 if (failed == 1) {
86 System.err.println(l + " .equals(" + r + ") => " + result +
87 "\n\tExpected " + expected);
88 }
89 return failed;
90 }
91 }